import {useCallback, useState} from 'react'

type Reducer <S, T> = (state:S, action:T) => S

const useStateReducer = <T, S>(reducer: Reducer<S, T>, state: S) => {
  const [store, setStore] = useState<S>(state)
  const dispatch = useCallback( (action: T) => {
    setStore(()=> reducer(store, action))
  }, [store])
  return [store, dispatch]
}
export default useStateReducer
